package icu.honeysuccess.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import icu.honeysuccess.canal.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Honey
 * @date 2022/3/29
 */
@CanalEventListener   // canal的监听类
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @ListenPoint(schema = "ydles_business",table = "tb_ad")
    public void adUpload(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        System.err.println("广告数据发生变化");

        rowData.getBeforeColumnsList().forEach(c -> System.out.println("改变之前" + c.getName()));

        System.out.println("===============================");

        rowData.getAfterColumnsList().forEach(c -> System.out.println("改变之后" + c.getName()));

        // 获得修改后的数据
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        for (CanalEntry.Column column : afterColumnsList) {
            if ("position".equals(column.getName())) {
                // 获取position的值，发送给消息队列
                String value = column.getValue();
                rabbitTemplate.convertAndSend("", RabbitMQConfig.AD_UPDATRE_QUEUE,value);
            }
        }


    }




}
